﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Product;
using NHibernate;
using BrokerBase.Lib.Base;
using BrokerBase.Lib.Tool;

namespace Broker.View.PolicyView
{
    /// <summary>
    /// PolicyLightControl.xaml 的交互逻辑
    /// </summary>
    public partial class PolicyLightControl : PolicyBaseControl
    {
        private IPolicyLight policyLight;

        public PolicyLightControl()
            : this(null)
        {

        }

        public PolicyLightControl(IPolicyLight policyLight)
            : base(policyLight)
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(PolicyLightControl_Loaded);

            LostFocus += new RoutedEventHandler(PolicyLightControl_LostFocus);

            this.policyLight = policyLight;
        }

        void PolicyLightControl_LostFocus(object sender, RoutedEventArgs e)
        {
            int x = 100;
        }

        void PolicyLightControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitEnumData();
            LoadTimeUnit();

            DataContext = this.policyLight;

            cmbPaymentTimeUnit.IsEnabled = false;//目前只支持年付

            if (policyLight.ProductType == ProductType.Basic)
            {
                rbtBasic.IsChecked = true;
            }
            else
            {
                rbtExtra.IsChecked = true;
            }
        }

        public override bool Verify()
        {
            if (string.IsNullOrEmpty(policyLight.ProductName))
            {
                MessageBox.Show("产品名称不能为空!", "提示");
                this.policyLight.IsVerify = false;
                return false;
            }

            if ((policyLight.InsurancePeriodStyle == InsurancePeriodStyle.Age || policyLight.InsurancePeriodStyle == InsurancePeriodStyle.Period) && policyLight.InsurancePeriodValue == 0)
            {
                MessageBox.Show("保险期限类型等于[年龄],[期间]时，保险期限不能等于空", "提示");
                this.policyLight.IsVerify = false;
                return false;

            }

            if ((policyLight.PaymentPeriodStyle == PaymentPeriodStyle.Age || policyLight.PaymentPeriodStyle == PaymentPeriodStyle.Period) && policyLight.PaymentPeriodValue == 0)
            {
                MessageBox.Show("付款期限类型等于[年龄],[期间]时，付款期限不能等于空", "提示");
                this.policyLight.IsVerify = false;
                return false;

            }

            if (policyLight.PaymentPeriodLength > policyLight.InsurancePeriodLength)
            {
                MessageBox.Show("付款期不能大于保险期", "提示");
                this.policyLight.IsVerify = false;
                return false;
            }

            if (policyLight.SelectedTimeUnit == null)
            {
                MessageBox.Show("付款周期不能为空!", "提示");
                this.policyLight.IsVerify = false;
                return false;
            }

            IPolicyLight bpl = policyLight.Owner.BasicPolicyLight;
            if (policyLight.ProductType == ProductType.Basic && bpl != null && bpl.Id != policyLight.Id)
            {
                MessageBox.Show("一个保险方案只能有一个主险！", "提示");
                this.policyLight.IsVerify = false;
                return false;
            }

            this.policyLight.IsVerify = true;
            return true;
        }

        public override void ClearData()
        {
            throw new NotImplementedException();
        }
        private void LoadTimeUnit()
        {
            ISession sess = LibGlobal.HbmSessionFactory.GetCurrentSession();

            IList<ITimeUnit> tus = sess.CreateQuery("from TimeUnit").List<ITimeUnit>();

            cmbPaymentTimeUnit.ItemsSource = tus;
        }

        private void InitEnumData()
        {
            InsurancePeriodStyleClass[] insStyles = new InsurancePeriodStyleClass[2]
            {
                new InsurancePeriodStyleClass {Name ="期间",PeriodStyle=InsurancePeriodStyle.Period},
                new InsurancePeriodStyleClass{Name="到年龄",PeriodStyle=InsurancePeriodStyle.Age}
                
            };

            cmbInsurancePeriodType.ItemsSource = insStyles;

            PaymentPeriodStyleClass[] payStyles = new PaymentPeriodStyleClass[2]
            {
                new PaymentPeriodStyleClass{Name="期间",PeriodStyle =PaymentPeriodStyle.Period},
                new PaymentPeriodStyleClass{Name ="到年龄",PeriodStyle=PaymentPeriodStyle.Age}
                
            };

            cmbPaymentPeriodType.ItemsSource = payStyles;

            UnitTypeClass[] unitTypes = new UnitTypeClass[2]
            {
                new UnitTypeClass{Name="金额", UnitType=UnitType.Amt},
                new UnitTypeClass{Name="数量",UnitType =UnitType.Qty}
            };

            cmbUnitType.ItemsSource = unitTypes;
        }
    }

    class InsurancePeriodStyleClass
    {
        public string Name { get; set; }

        public InsurancePeriodStyle PeriodStyle { get; set; }
    }

    class PaymentPeriodStyleClass
    {
        public string Name { get; set; }

        public PaymentPeriodStyle PeriodStyle { get; set; }
    }

    class UnitTypeClass
    {
        public string Name { get; set; }

        public UnitType UnitType { get; set; }
    }
}
